<?php
/**
 * 价格变化规则的类，获取现价和规则进行计算
 * @author Administrator
 * @create 2013-4-19 13:57:54
 */
class Subscribe_Push_Criteria
{
	private $_baseArr;
	public function isInRange($price, $basicArr)
	{
		$this->_baseArr = $basicArr;
		
		//如果股价为0说明有问题
		if ($price == 0) {
			return array(
				'isInRange' => false,
				'code' => $basicArr['code'],
				'price' => $price,
			);
		}
		
		$maxDays = Zend_Registry::get('dzlalert')->day->range;
		
		$checkObj = new Subscribe_Push_Cron_Checker();
		
		//判断是否已经提醒了买入
		if ($this->_isEmptyDate($basicArr['mrdate'])) {
			if ($basicArr['date'] != date('Y-m-d')) {
				return array(
					'isInRange' => false,
					'code' => $basicArr['code'],
					'price' => $price,
				);
			} else {
				$preRange = Zend_Registry::get('dzlalert')->pre->range;
				$mrj = $basicArr['mrj'];
				$topMrj = (string)((1 + $preRange) * $mrj);
				
				//到达预买入价了
				if ($price > $mrj && $price <= $topMrj && $this->_isEmptyDate($basicArr['premrdate'])) {
					//更新数据库的买入价日期
					return $this->_dbOperationAndReturn('premr', $basicArr['code'], $price);
				}
				
				//到达买入价了
				if ($price <= $mrj && $this->_isEmptyDate($basicArr['mrdate'])) {
					//更新数据库的买入价日期
					return $this->_dbOperationAndReturn('mr', $basicArr['code'], $price);
				}
			}
		} elseif ($basicArr['mrdate'] != date('Y-m-d') 
		&& $checkObj->countDate($basicArr['mrdate'], date('Y-m-d')) <= $maxDays) {
			//买入价达到了，但是当天不提示
			$preRange = Zend_Registry::get('dzlalert')->pre->range;
			$zyj = $basicArr['zyj'];
			$zsj = $basicArr['zsj'];
			$bottomZyj= (string)((1 - $preRange) * $zyj);
			$topZsj= (string)((1 + $preRange) * $zsj);
			
			//到达预止盈价了
			if ($price >= $bottomZyj && $price < $zyj && ($this->_isEmptyDate($basicArr['prezydate']) 
			|| $basicArr['prezydate'] != date('Y-m-d')) && $this->_isEmptyDate($basicArr['zydate']) 
			&& $this->_isEmptyDate($basicArr['zsdate'])) {
				//更新数据库的买入价日期
				return $this->_dbOperationAndReturn('prezy', $basicArr['code'], $price);
			}
			
			//到达止盈价了
			if ($price >= $zyj && $this->_isEmptyDate($basicArr['zydate']) 
			&& $this->_isEmptyDate($basicArr['zsdate'])) {
				//更新数据库的买入价日期
				return $this->_dbOperationAndReturn('zy', $basicArr['code'], $price);
			}
			
			//到达预止损价了
			if ($price > $zsj && $price <= $topZsj && ($this->_isEmptyDate($basicArr['prezsdate']) 
			|| $basicArr['prezsdate'] != date('Y-m-d')) && $this->_isEmptyDate($basicArr['zsdate']) 
			&& $this->_isEmptyDate($basicArr['zydate'])) {
				//更新数据库的买入价日期
				return $this->_dbOperationAndReturn('prezs', $basicArr['code'], $price);
			}
			
			//到达止损价了
			if ($price <= $zsj && $this->_isEmptyDate($basicArr['zydate']) 
			&& $this->_isEmptyDate($basicArr['zsdate'])) {
				//更新数据库的买入价日期
				return $this->_dbOperationAndReturn('zs', $basicArr['code'], $price);
			}
		}
		
		//什么都没有的返回值
		return array(
			'isInRange' => false,
			'code' => $basicArr['code'],
			'price' => $price,
		);
	}

	private function _dbOperationAndReturn($type, $code, $currPrice)
	{
		$typeDate = $type . 'date';
		//更新数据库的日期
		$binds = array($typeDate => date('Y-m-d'));
		$where = array(
			'code=?' => $code,
			'style=?' => $this->_baseArr['style']
		);
		Dzl_Db_Factory::getInstance()->createTable('Subscribe_stocks')->update($binds, $where);
		//返回信息
		if (strstr($type, 'pre')) {
			$anotherType = substr($type, 3);
		} else {
			$anotherType = $type;
		}
		$pointPriceType = $anotherType . 'j';
		return array(
			'isInRange' => true,
			'type' => $type,
			'code' => $code,
			'currentPrice' => $currPrice,
			'date' => $this->_baseArr['date'],
			'pointPrice' => $this->_baseArr[$pointPriceType]
		);
	}
	
	private function _isEmptyDate($date)
	{
		return ($date == null || $date == '0000-00-00') ? true : false;
	}
}